import numpy as np
import csv
from sklearn.decomposition import PCA,KernelPCA,SparsePCA
from sklearn.ensemble import ExtraTreesClassifier,RandomForestClassifier,AdaBoostClassifier,GradientBoostingClassifier
from sklearn import svm,tree,neighbors
from sklearn.metrics import f1_score
from sklearn import preprocessing
from sklearn.neural_network import BernoulliRBM

def load_data_csv(file):
	lines = csv.reader(open(file))
	data = []
	for line in lines:
		data.append(line)
	data=np.asarray(data)
	return data

data = load_data_csv('task2/train_data.csv')

X=data[:,:410].astype(float)
Y=data[:,410].astype(int)

test = load_data_csv('task2/test_feature_data.csv')

test = test[:,1:].astype(float)

'''
scaler = preprocessing.StandardScaler().fit(X)
X=scaler.transform(X)
test=scaler.transform(test)
'''


'''
pca=PCA(n_components=100)
pca.fit(X)
xx=pca.transform(X)
tt=pca.transform(test)


rbm = BernoulliRBM(n_components=100)
rbm.fit(X)
xx=rbm.transform(X)
tt=rbm.transform(test)


kpca = KernelPCA(kernel="rbf", fit_inverse_transform=True, gamma=10)
kpca.fit(xx)
xx=kpca.transform(xx)
tt=kpca.transform(tt)
'''

'''
clf = ExtraTreesClassifier(n_estimators=100).fit(X, Y)
pp = clf.predict(test)
print pp
k=1
f = open("task2_pre.csv", "w")
for p in pp:
	print >> f,"%d,%d" %(k,p)
	k += 1
f.close()
'''
n_neighbors = 15
weights='uniform'#'distance' # 'uniform'
from sklearn import cross_validation
from sklearn.cross_validation import KFold
kf = KFold(len(Y), n_folds=3)
#model=ExtraTreesClassifier(n_estimators=100) # Accuracy: 0.72 (+/- 0.04)
#model=svm.SVC()
#model=ExtraTreesClassifier(n_estimators=500)
#model=RandomForestClassifier(n_estimators=100)
model=GradientBoostingClassifier(n_estimators=30,max_depth=6,learning_rate=0.2)
#model = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
#model=AdaBoostClassifier(n_estimators=100)
#model=tree.DecisionTreeClassifier()
#accuracy = cross_validation.cross_val_score(model, X, Y, cv=kf)
accuracy = cross_validation.cross_val_score(model, X, Y, cv=kf, scoring='f1')
print accuracy
print("Accuracy: %0.2f (+/- %0.2f)" % (accuracy.mean(), accuracy.std() * 2))

